package com.miao.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.miao.io.Resources;
import com.miao.pojo.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * @author 喵瑞恒
 * @date 2023/09/13 20:48
 **/
public class XMLConfigBuilder {


    private Configuration configuration;

    public XMLConfigBuilder() {
        this.configuration = new Configuration();
    }

    /**
     * 使用dom4j + xpath解析配置文件，封装Configuration对象
     *
     * @param inputStream
     * @return
     */
    public Configuration parse(InputStream inputStream) throws DocumentException {

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        Element rootElement = document.getRootElement();
        List<Element> list = rootElement.selectNodes("//property");

        // <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        Properties properties = new Properties();
        for (Element element : list) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.setProperty(name, value);
        }

        //创建数据源对象
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getProperty("driverClassName"));
        druidDataSource.setUrl(properties.getProperty("url"));
        druidDataSource.setUsername(properties.getProperty("username"));
        druidDataSource.setPassword(properties.getProperty("password"));

        //将创建好的数据源对象封装到Configuration对象中
        configuration.setDataSource(druidDataSource);

        //-------------------解析映射配置文件---------------------
        //1.获取映射配置文件的路径   2.根据路径进行映射配置文件的加载和解析  3.封装到MappedStatement对象，然后封装到Configuration对象的map集合中
        List<Element> mapperList = rootElement.selectNodes("//mapper");

        // <mapper resource="mapper/UserMapper.xml"></mapper>
        for (Element element : mapperList) {
            String mapperPath = element.attributeValue("resource");
            InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath);
            //专门解析映射配置文件的对象
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
            xmlMapperBuilder.parse(resourceAsStream);
        }
        return configuration;
    }
}
